//
//  MineDetailViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MineDetailViewController.h"
#import "ActivityModel.h"
#import "MovieModel.h"
#import "MovieDetailViewController.h"
#import "activityDetailViewController.h"
@interface MineDetailViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSMutableArray *dataArr;// 用于存放放Model的数组
@end

@implementation MineDetailViewController

//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Cell" object:nil];
//}

//-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        //设置当前页面为一个人监听者 当前页面监视之前的页面
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(add:) name:@"Cell" object:nil];
//    }
//    return self;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    
    // 初始化tableView
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    // 获取文件夹路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:_fileStr];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    NSString *namePath = [path stringByAppendingPathComponent:str];
    NSString *detailPath = [namePath stringByAppendingPathComponent:_nameStr];
    // 获取数组
    
    self.dataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:detailPath] ;
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

//实现监听的方法

//-(void)add:(NSNotification *)notification
//{
//    NSLog(@"监听方法");
//    NSDictionary *nameDic = notification.userInfo;
//    self.label.text = [nameDic objectForKey:@"name"];
//}


// cell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if ([_Juge isEqualToString:@"0"]) {
        NSLog(@"000");
        ActivityModel *m = _dataArr[indexPath.row];
        cell.textLabel.text = m.title;
    }else if ([_Juge isEqualToString:@"1"]){
        MovieModel *m = _dataArr[indexPath.row];
        cell.textLabel.text = m.title;
    }
    
    return cell;
}

// 点击cell进入详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 判断是电影还是活动
    if ([_Juge isEqualToString:@"0"]) {
        activityDetailViewController *activityVC = [activityDetailViewController new];
        activityVC.model = self.dataArr[indexPath.row];
        [self.navigationController pushViewController:activityVC animated:YES];
    }else if ([_Juge isEqualToString:@"1"]){
        MovieDetailViewController *movieVC = [MovieDetailViewController new];
        movieVC.moedel = _dataArr[indexPath.row];
        [self.navigationController pushViewController:movieVC animated:YES];
    }
}

///是否编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

///确定cell的编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

/// 编辑按钮的触发
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableview setEditing:editing animated:YES];
}

/// 删除确认信息设置
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

/// 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if ([_Juge isEqualToString:@"0"]) {
            //活动
            // 将本地文件中的数组中所对应的Model删除
            NSString *ActivityPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"activity"];
            NSLog(@"%@", NSHomeDirectory());
            [_dataArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:_dataArr toFile:[ActivityPath stringByAppendingPathComponent:@"activity.plist"]];
            // 视图同步
            [_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }else if([_Juge isEqualToString:@"1"]){
            // 电影
            NSString *MoviePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"movie"];
            [_dataArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:_dataArr toFile:[MoviePath stringByAppendingPathComponent:@"movie.plist"]];
            [_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            
        }
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
